<?php
    if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
	
	// Set up mysqli connection
	require_once('db_connect.php');
	if(isset($_COOKIE['authData'])){
	// Delete session
	$query = $mysqli->prepare("DELETE FROM SESSIONS WHERE session_key = ?;");
	$query->bind_param("s", $sessionkey);
	$sessionkey = $_COOKIE['authData'];
	$query->execute();
	$mysqli->close();
}
	//session_destroy();
	session_unset();
	unset($_SESSION['loggedon']);
	$_SESSION['last_action'] = 0;
	$_SESSION['userid']  = -1;
	$_SESSION['username'] = '';
	$_SESSION['userlevel'] = 0;
	
	// unset cookie
	setcookie('authData', '', time()-3600, '/');
	
	header("Location: ../index.php");
?>